﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBConnect;
using System.IO;

namespace TISS_Add_in
{
    public partial class uc_NumeroGuia : UserControl
    {
        public delegate void numeroGuiaChangedHandler(object sender);
        public event numeroGuiaChangedHandler numeroGuiaChanged;

        #region Properties

        private TreeNodeContents nodeContents = new TreeNodeContents();
        public TreeNodeContents NodeContents
        {
            get { return nodeContents; }
            set { nodeContents = value; }
        }

        #endregion

        public uc_NumeroGuia()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            NodeContents.numeroGuia = txtBoxNumeroGuia.Text;
            NodeContents.numeroGuia = NodeContents.numeroGuia.PadLeft(20, '0');
            Int64 ID = 0;
             ID = Globals.ThisAddIn.UsuarioID;
             if (ID != 0)
             {
                 int i = 0;
                 if (nodeContents.tipo_node == node_type.lote)
                     i = CommandGuias.verificaNumeroGuia(ID, nodeContents.regANS, NodeContents.numeroGuia);
                 else
                     i = CommandSolicitacao.verificaNumeroGuia(nodeContents.regANS, ID, NodeContents.numeroGuia);
                 if (i > 0)
                     MessageBox.Show("Este número de guia já existe!", "Atenção!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                 else
                 {
                     numeroGuiaChanged(this);
                     Globals.ThisAddIn.CustomTaskPanes.Remove(Globals.ThisAddIn.NumeroGuiaCTP);
                 }
             }
             else
             {
                 numeroGuiaChanged(this);
                 Globals.ThisAddIn.CustomTaskPanes.Remove(Globals.ThisAddIn.NumeroGuiaCTP);
             }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Globals.ThisAddIn.CustomTaskPanes.Remove(Globals.ThisAddIn.NumeroGuiaCTP); 
                   
            try
            {
                string pathGuia = Globals.ThisAddIn.Application.ActiveDocument.FullName;

                object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; 
                object mis = Globals.ThisAddIn._missing;
                Globals.ThisAddIn.Application.ActiveDocument.Close(ref doNotSaveChanges, ref mis, ref mis);               
                File.Delete(pathGuia);
            
            }
            catch (Exception)
            {
                
                throw;
            }         
        }

        private void uc_NumeroGuia_Load(object sender, EventArgs e)
        {
            txtBoxNumeroGuia.Focus();
        }

        private void txtBoxNumeroGuia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar.CompareTo((char)Keys.Return)) == 0)
            {
                e.Handled = true;
                btnOK_Click(this, e);
            }
        }               
    }
}
